<template>
  <div class="fixed-container login-container">
    <div class="login-form">
      <img src="./images/logo_index.png" class="login-logo" />
      <el-form :model="loginData" ref="loginForm" :rules="rules">
        <el-form-item prop="mobile">
          <el-input
            v-model="loginData.mobile"
            placeholder="请输入手机号码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="loginData.code"
            placeholder="请输入验证码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="agree">
          <el-checkbox v-model="loginData.agree"
            >我已阅读并同意用户协议</el-checkbox
          >
        </el-form-item>
        <el-form-item>
          <el-button :loading="isLogin" type="primary" class="login-btn" @click="onLogin"
            >登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/user";
export default {
  name: "Login",
  data() {
    return {
      loginData: {
        agree: true,
      },
      rules: {
        mobile: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          {
            pattern: /^1[3|5|7|8|9]\d{9}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          {
            pattern: /^\d{6}$/,
            message: "请输入正确的验证码",
            trigger: "blur",
          },
        ],
        agree: [
          {
            validator: (rule, val, callback) => {
              if (!val) {
                return callback(new Error("请先已阅读并同意用户协议"));
              }
              return callback();
            },
            trigger: "blur",
          },
        ],
      },
      isLogin: false
    };
  },
  methods: {
    login() {
      this.isLogin = true
      login(this.loginData).then((res) => {
        this.isLogin = false
        localStorage.setItem("user", JSON.stringify(res.data.data));
        this.$router.push('/')
      });
    },
    onLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.login();
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  background: url("./images/login_bg.jpg") no-repeat;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
  .login-form {
    background: #fff;
    padding: 40px 50px 20px;
    .login-logo {
      margin-bottom: 20px;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
